package com.weimob.mcloud.wechatwork.customer.job;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.weimob.elasticjob.annotation.ElasticJob;
import com.weimob.mcloud.wechatwork.core.adapter.pojo.dto.ac.req.BWidChangeSecondMsg;
import com.weimob.mcloud.wechatwork.core.base.SingleResponse;
import com.weimob.mcloud.wechatwork.core.entity.privatesphere.PrivateSphere;
import com.weimob.mcloud.wechatwork.core.enums.account.BWidChangeHandleStatusEnum;
import com.weimob.mcloud.wechatwork.core.service.account.BWidChangeRecordCoreService;
import com.weimob.mcloud.wechatwork.core.service.account.request.BWidChangeRecord;
import com.weimob.mcloud.wechatwork.core.service.account.request.SingleBWidChangeRecordRequest;
import com.weimob.mcloud.wechatwork.core.service.privatesphere.PrivateSphereService;
import com.weimob.mcloud.wechatwork.customer.domain.ArtemisBaseInfo;
import com.weimob.mcloud.wechatwork.customer.domain.ArtemisConstant;
import com.weimob.mcloud.wechatwork.customer.service.impl.ArtemisService;
import com.weimob.soa.common.response.SoaResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Objects;

/**
 * 功能描述:
 *
 * @author lvjunlong
 * @date 2023/6/19 下午2:20
 */
@Slf4j
@Component("bWidChangeSecondJob")
public class BWidChangeSecondJob {

    @Autowired
    private BWidChangeRecordCoreService bWidChangeRecordCoreService;

    @Reference
    private PrivateSphereService privateSphereService;

    @Autowired
    private ArtemisService artemisService;

    @ElasticJob(name = "bWidChangeFailRecord", cron = "0 0 2 * * ?")
    public void bWidChangeFailRecord() {

        log.info("bWidChangeFailRecord job");

        Long nextId = 0L;
        while (true) {
            SingleBWidChangeRecordRequest<BWidChangeRecord> scrollRequest = new SingleBWidChangeRecordRequest<>();
            scrollRequest.setNextId(nextId);
            scrollRequest.setLimit(50);
            scrollRequest.setStatus(BWidChangeHandleStatusEnum.FAIL.getStatus());

            SoaResponse<SingleResponse<List<BWidChangeRecord>>, Void> soaResponse = bWidChangeRecordCoreService.scroll(scrollRequest);
            if (0L != Long.parseLong(soaResponse.getReturnCode()) || Objects.isNull(soaResponse.getResponseVo())) {
                log.error("滚动查询bWid变更记录失败, request:{}, response:{}", JSON.toJSONString(scrollRequest),
                        JSON.toJSONString(soaResponse));
                break;
            }

            List<BWidChangeRecord> bWidChangeRecordList = soaResponse.getResponseVo().getData();
            if (CollectionUtils.isEmpty(bWidChangeRecordList)) {
                break;
            }

            nextId = bWidChangeRecordList.get(bWidChangeRecordList.size() - 1).getId();
            if (Objects.isNull(nextId)) {
                log.error("滚动查询bWid变更记录, nextId为空");
                break;
            }

            sendMsg(bWidChangeRecordList);
        }
    }

    private void sendMsg(List<BWidChangeRecord> bWidChangeRecordList) {
        if (CollectionUtils.isEmpty(bWidChangeRecordList)) {
            return;
        }

        for (BWidChangeRecord bWidChangeRecord : bWidChangeRecordList) {

            PrivateSphere privateSphereRequest = new PrivateSphere();
            privateSphereRequest.setBosId(bWidChangeRecord.getBosId());
            privateSphereRequest.setProductId(203L);
            SoaResponse<PrivateSphere, Void> soaResponse = privateSphereService.simpleFetch(privateSphereRequest);
            if (0L != Long.parseLong(soaResponse.getReturnCode()) || Objects.isNull(soaResponse.getResponseVo())) {
                log.warn("查询私域失败, bosId:{}, result:{}", bWidChangeRecord.getBosId(), JSON.toJSONString(soaResponse));
                continue;
            }

            PrivateSphere privateSphere = soaResponse.getResponseVo();

            BWidChangeSecondMsg bWidChangeSecondMsg = new BWidChangeSecondMsg();
            bWidChangeSecondMsg.setPrivateSphereId(privateSphere.getPrivateSphereId());
            bWidChangeSecondMsg.setBosId(bWidChangeRecord.getBosId());
            bWidChangeSecondMsg.setCorpId(privateSphere.getCorpId());
            bWidChangeSecondMsg.setWid(bWidChangeRecord.getWid());
            bWidChangeSecondMsg.setOriginalWid(bWidChangeRecord.getOriginalWid());
            bWidChangeSecondMsg.setMsgId(bWidChangeRecord.getMsgId());
            bWidChangeSecondMsg.setTabName(bWidChangeRecord.getTabName());

            ArtemisBaseInfo<BWidChangeSecondMsg> artemisBaseInfo = new ArtemisBaseInfo<>();
            artemisBaseInfo.setTopic(ArtemisConstant.Topic.WECHATWORK_BOS_WID_CHANGE);
            artemisBaseInfo.setEvent(bWidChangeSecondMsg);
            artemisBaseInfo.setProducer(ArtemisConstant.PRODUCT_USER);
            log.info("扫描bWid变更记录失败的数据, magBody:{}", JSON.toJSONString(artemisBaseInfo));
            Boolean sendBoolean = artemisService.sendToArtemis(artemisBaseInfo);
        }
    }
}
